package com.wushi.lenovo.asia5b.app_ui.shopcar.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TextView;

import com.mvp.base.BaseFragmentPagerAdapter;
import com.mvp.base.BaseMvpActivity;
import com.mvp.ui.view.HeadBar;
import com.wushi.lenovo.asia5b.R;
import com.wushi.lenovo.asia5b.app_di.APPComponentFactory;
import com.wushi.lenovo.asia5b.app_di.goods.mvpview.ShopCarActivityView;
import com.wushi.lenovo.asia5b.app_di.goods.presenter.ShopCarActivityPresenter;
import com.wushi.lenovo.asia5b.app_event.ShopCarCoutEvent;
import com.wushi.lenovo.asia5b.app_ui.shopcar.fragment.ShopCarFragment;
import com.wushi.lenovo.asia5b.app_ui.shopcar.fragment.ShopCarPiFragment;
import com.wushi.lenovo.asia5b.app_utils.eye.EyeUtils;
import com.wushi.lenovo.asia5b.app_widget.viewpager.NoScrollViewPager2;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by zhangyuncai on 2019/10/30.
 * 购物车页面
 * todo 购物车中的商品有自营/代购(自己的/第三方的)  普通/敏感(计算运费方式不同) 的区别
 */
public class ShopCarActivity extends BaseMvpActivity<ShopCarActivityView, ShopCarActivityPresenter> implements ShopCarActivityView {
    @BindView(R.id.tab_shopcar)
    TabLayout tabLayout;
    @BindView(R.id.vp_shopcar)
    NoScrollViewPager2 mViewPager;
    @BindView(R.id.headbar_shopcar)
    HeadBar headbar_shopcar;
    private ShopCarPiFragment shopCarPiFragment;
    private ShopCarFragment shopCarFragment;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_shopcar;
    }

    @Override
    protected void constructComponent() {
        APPComponentFactory.getAppBaseComponent().inject(this);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        initView();

        initData();

        initEvent();
    }

    @Override
    protected void initView() {
        tabLayout.removeAllTabs();
        tabLayout.addTab(tabLayout.newTab().setCustomView(tabView(getString(R.string.shopcar_bulkbuy))));
        tabLayout.addTab(tabLayout.newTab().setCustomView(tabView(getString(R.string.shopcar_shopfromglobe))));
        //初始化viewpager
        List<Fragment> fragmentList = new ArrayList<>();
        shopCarPiFragment = (ShopCarPiFragment) ShopCarFragment.sNewStance(this, ShopCarPiFragment.class.getName());
        fragmentList.add(shopCarPiFragment);
        shopCarFragment = (ShopCarFragment) ShopCarFragment.sNewStance(this, ShopCarFragment.class.getName());
        fragmentList.add(shopCarFragment);
        BaseFragmentPagerAdapter fragmentPagerAdapter = new BaseFragmentPagerAdapter(getSupportFragmentManager(), fragmentList, null);
        mViewPager.setAdapter(fragmentPagerAdapter);
        mViewPager.setOffscreenPageLimit(2);
//        tabLayout.setupWithViewPager(mViewPager);

    }

    private View tabView(String tabName) {
        View view = View.inflate(this, R.layout.item_inbox_tabitem, null);
        TextView tv_inbox_inboxtitle = view.findViewById(R.id.tv_inbox_inboxtitle);
        TextView tv_inbox_inboxcount = view.findViewById(R.id.tv_inbox_inboxcount);
        tv_inbox_inboxtitle.setText(tabName);
        tv_inbox_inboxcount.setText("0");
        return view;
    }

    public void setTabCount(int index, int count) {
        try {
            TextView tv_inbox_inboxcount = tabLayout.getTabAt(index).getCustomView().findViewById(R.id.tv_inbox_inboxcount);
            EyeUtils.setNums(tv_inbox_inboxcount, count);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initEvent() {
        headbar_shopcar.findViewById(R.id.iv_title_right1)
                .setOnClickListener(v -> {
                    if (mViewPager.getCurrentItem() == 1 && shopCarFragment != null) {
                        shopCarFragment.delete();
                    } else if (mViewPager.getCurrentItem() == 0 && shopCarPiFragment != null) {
                        shopCarPiFragment.delete();
                    }
                });
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                mViewPager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                tabLayout.setScrollPosition(position, 0f, true);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onShopCarCoutEvent(ShopCarCoutEvent event) {

        if (event != null) {
            if (event.getIndex() == 0) {
                setTabCount(0, event.getCount());
            } else {
                setTabCount(1, event.getCount());
            }
        }

    }

    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }
}
